//
//  VisitRecordTableViewCell.m
//  YunGui
//
//  Created by wmm on 16/4/5.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "VisitRecordTableViewCell.h"
#import "SJAvatarBrowser.h"

#define LeftWidth [UIView getWidth:20.0f]+20.0f
#define LabelHeight 20.0f

const CGFloat TableCellHeight3 = 190.0f+10;

@interface VisitRecordTableViewCell ()
{
    float H;
    float W;
    
}

@end


@implementation VisitRecordTableViewCell

- (VisitRecordTableViewCell *)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier VisitRecordModel:(VisitRecordModel *)recordModel Frame:(CGRect) rect {
    
    _cell=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if(_cell!=nil){
        
        [_cell setFrame:CGRectMake(0, 0, kScreenWidth-45, 60)];
        
        CGFloat leftW = 15.0f;
        CGFloat upH = 5.0f;
        CGFloat picSize = [UIView getWidth:40.0f];
        CGFloat lineW = 5.0f;
        
        UIView *leftLine1 = [[UIView alloc] initWithFrame:CGRectMake(LeftWidth, 0.0f, lineW, 10.0f)];
        leftLine1.backgroundColor = LEFTLINECOLOR;
        leftLine1.tag = 67517;
        
        _quanImg = [[UIImageView alloc] initWithFrame:CGRectMake(LeftWidth-6.0f, leftLine1.maxY+lineW,15.0f, 15.0f)];
        _quanImg.image = [UIImage imageNamed:@"Btn_Normal_Shijianquan.png"];
        _quanImg.tag = 67518;
        
        _leftLine2 = [[UIView alloc] initWithFrame:CGRectMake(LeftWidth, _quanImg.maxY+lineW, lineW, TableCellHeight3-_quanImg.maxY-lineW)];
        _leftLine2.backgroundColor = LEFTLINECOLOR;
        _leftLine2.tag = 67519;
        
        [_cell.contentView addSubview:leftLine1];
        [_cell.contentView addSubview:_quanImg];
        [_cell.contentView addSubview:_leftLine2];
        
        //        CGRect recordFrame = CGRectMake(leftLine1.maxX+leftW, leftLine1.maxY+2.0f, 100.0f, LabelHeight);
        
        
        CGFloat startX = [UIView getWidth:20];
        CGFloat imgW = 20;
        CGFloat labX = startX + imgW +15;
        CGFloat labH = 40;
        CGFloat labY = 10;
        CGFloat height = labH - 2*labY;
        //        CGFloat grayH = 15;
        CGFloat labW  = 80;
        CGFloat tfX = labX + labW +[UIView getWidth:5];
        CGFloat tfW = kScreenWidth - tfX - 20;
        
        NSArray *labelArray=@[@"拜访日期",@"联系人",@"联系电话",@"拜访记录"];
        for (int i=0; i<labelArray.count; i++) {
            UILabel *label = [self getLabelWith:CGRectMake(labX, leftLine1.maxY+2.0f+ i*(height+5), labW, height) WithTitle:labelArray[i] WithFontSize:14.0f WithTitleColor:GRAYCOLOR WithTextAlignment:NSTextAlignmentLeft];
            label.tag = 989800+ i;
            [_cell.contentView addSubview:label];
        }
        
        
        _visitDate = [self getLabelWith:CGRectMake(tfX, leftLine1.maxY+2.0f, tfW, LabelHeight) WithTitle:recordModel.created_at];
        
        
        _contactLab = [self getLabelWith:CGRectMake(tfX, _visitDate.maxY+5.0f, tfW, LabelHeight) WithTitle:recordModel.contact];
        
        _phoneLab = [self getLabelWith:CGRectMake(tfX, _contactLab.maxY + 5.0f, tfW, LabelHeight) WithTitle:recordModel.tel];
        
        
        [_cell.contentView addSubview:_visitDate];
        [_cell.contentView addSubview:_contactLab];
        [_cell.contentView addSubview:_phoneLab];
        
        
        CGRect visitRecordFrame = CGRectMake(tfX, _phoneLab.maxY + 5, tfW, 40);
        
        _label = [[UILabel alloc]initWithFrame:visitRecordFrame];
        
        NSString *text = recordModel.desc;
        text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];  //去除掉首尾的空白字符和换行字符
        NSLog(@"text：%@",text);
        [_label setText:text];
        
        _label.font=[ViewTool getFont:15.0f];
        _label.textColor = BLACKCOLOR;
        [_cell.contentView addSubview:_label];
        
        CGSize titleSize = [text boundingRectWithSize:CGSizeMake(visitRecordFrame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[ViewTool getFont:15.0f]} context:nil].size;
        
        H=titleSize.height+_label.y+picSize +25;
        
//        _btn =[[MyButton alloc]initWithFrame:CGRectMake(_label.maxX - 25, _label.maxY+upH, 20, 20)];
//        [_cell.contentView addSubview:_btn];
//        if (titleSize.height>40) {
//            
//            _label.height = 40;
//            _label.numberOfLines=2;
//            [_label sizeToFit];
//            
//            UIImage *image = [UIImage imageNamed:@"Btn_Normal_Jiantouxia"];
//            [_btn setImage:image forState:UIControlStateNormal];
//            
//            _btn.hidden = NO;
//            [_btn setIsEXP:YES];
//        }else{
//            //不需要 展开按钮
//            //            _label.numberOfLines=0;
//            if (titleSize.height>20) {
//                _label.height = 40;
//                _label.numberOfLines=2;
//            }
//            if (20>titleSize.height>0) {
//                _label.height = 20;
//            }
//            
//            [_btn setIsEXP:NO];
//            _btn.hidden = YES;
//        }
        
        _picView = [[UIView alloc] initWithFrame:CGRectMake(_label.x, _label.maxY+upH, _label.width-25, picSize)];
        
        [_cell.contentView addSubview:_picView];
        _visitPic1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, picSize, picSize)];
        _visitPic1.userInteractionEnabled = YES;
        [_picView addSubview:_visitPic1];
        
        _visitPic2 = [[UIImageView alloc] initWithFrame:CGRectMake(_visitPic1.maxX+10, _visitPic1.y, picSize, picSize)];
        _visitPic2.userInteractionEnabled = YES;
        [_picView addSubview:_visitPic2];
        
        _visitPic3 = [[UIImageView alloc] initWithFrame:CGRectMake(_visitPic2.maxX+10, _visitPic1.y, picSize, picSize)];
        _visitPic3.userInteractionEnabled = YES;
        [_picView addSubview:_visitPic3];
        
        
        [self setImages:recordModel.images];
        
        _bottomLine = [ViewTool getLineViewWith:CGRectMake(LeftWidth+leftW, TableCellHeight3-1, kScreenWidth-_leftLine2.maxX-upH*2, 1.0f) withBackgroudColor:LEFTLINECOLOR];
        
        [_cell.contentView addSubview:_bottomLine];
    }

    return _cell;
    
}
- (void)setRecordModel:(VisitRecordModel *)recordModel
{
    _recordModel = recordModel;
//    if (_visitDate) {
//        [_visitDate removeFromSuperview];
//        
//    }
//    if (_contactLab) {
//        [_contactLab removeFromSuperview];
//    }
//    if (_phoneLab) {
//        [_phoneLab removeFromSuperview];
//    }
//    if( _label){
//        [_label removeFromSuperview];
//    }
//    if(_picView){
//        [_picView]
//    }
    
}
-(void)setImages:(NSArray *)imagesArray{
    if (imagesArray.count > 0) {
        [_visitPic1 sd_setImageWithURL:[NSURL URLWithString:imagesArray[0]] placeholderImage:nil];
//        NSLog(@"images[0]*-*-*-8989898*-**-*-*：%@",imagesArray[0]);
        UITapGestureRecognizer *tap1  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage1)];
        [_visitPic1 addGestureRecognizer:tap1];
        if (imagesArray.count > 1) {
            [_visitPic2 sd_setImageWithURL:[NSURL URLWithString:imagesArray[1]] placeholderImage:nil];
            UITapGestureRecognizer *tap2  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage2)];
            [_visitPic2 addGestureRecognizer:tap2];
            
            if (imagesArray.count > 2) {
                [_visitPic3 sd_setImageWithURL:[NSURL URLWithString:imagesArray[2]] placeholderImage:nil];
                UITapGestureRecognizer *tap3  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage3)];
                [_visitPic3 addGestureRecognizer:tap3];
            }else{
                [_visitPic3 sd_setImageWithURL:nil placeholderImage:nil];
            }
        }else{
            [_visitPic2 sd_setImageWithURL:nil placeholderImage:nil];
            [_visitPic3 sd_setImageWithURL:nil placeholderImage:nil];
        }
    }
    
}

- (void)magnifyImage1
{
    NSLog(@"局部放大");
    if(!(_visitPic1.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic1];//调用方法
    }
}
- (void)magnifyImage2
{
    NSLog(@"局部放大");
    if(!(_visitPic2.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic2];//调用方法
    }
}
- (void)magnifyImage3
{
    NSLog(@"局部放大");
    if(!(_visitPic3.image == NULL)){
        [SJAvatarBrowser showImage:_visitPic3];//调用方法
    }
}

- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title WithFontSize:(CGFloat)fontSize WithTitleColor:(UIColor *)color WithTextAlignment:(NSTextAlignment )textAlignment
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = color;
    label.textAlignment = textAlignment;
    label.font = [ViewTool getFont:fontSize];
    return label;
}

- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = BLACKCOLOR;
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [ViewTool getFont:15.0f];
    return label;
}

//-(void)cellBtnClick:(MyButton *)sender{
//
//    NSLog(@"cellBtnClick");
//
//}

-(float)returnRowHeight{
    return H;
}
-(id)returnBtn{
    if (_btn!=nil) {
        return _btn;
    }
    return nil;
}

-(BOOL)getBtnHidde{
    return _btn.hidden;
}

@end